#ifndef DOMINION_H_INCLUDED
#define DOMINION_H_INCLUDED
#include "dominion.h"
#endif

#ifndef TIME_H_INCLUDED
#define TIME_H_INCLUDED
#include <time.h>
#endif


#include "randomutils.h"
#include <stdio.h>

int num_copper_hand(int player, struct gameState *G)
{

	int num_copper = 0;

	int i;
	for(i = 0; i < G->handCount[player]; i++)
	{
		if(G->hand[player][i] == copper) 
		{
			num_copper++;
		}
	}
	return num_copper;
}

int num_copper_discard(int player, struct gameState *G)
{

	// init a 0 set array
	int num_copper = 0;

	int i;
	for(i = 0; i < G->discardCount[player]; i++)
	{
		if(G->discard[player][i] == copper)
		{
			num_copper++;
		}
	}
	return num_copper;
}

int coins_in_hand(int player, struct gameState *G)
{
	int coins = 0;
  //add coins for each Treasure card in player's hand
  int i;
	for (i = 0; i < G->handCount[player]; i++)
    {
      if (G->hand[player][i] == copper)
	{
	  coins += 1;
	}
      else if (G->hand[player][i] == silver)
	{
	  coins += 2;
	}
      else if (G->hand[player][i] == gold)
	{
	  coins += 3;
	}	
    }
	return coins;
}

int testCutpurse()
{
	// initialize the random game
	struct gameState* G = random_game_state();

	//decide which player we wish to have play
	int player = rand() % G->numPlayers;	

	// make sure we are in the right state for this player
	// to play
	setup_for_player(player, G);
	//randomize all player's cards(as we need to dispose of copper)
	int i;
	for(i = 0; i < G->numPlayers; i++)
	{
		random_deck(i, G);
		random_discard(i, G);
		random_hand(i, G);
	}

	// determine where to put an cutpurse 
	int ctp_pos;
	if(G->handCount[player] == 0)
	{
		ctp_pos = 0;
	}
	else
	{
		ctp_pos = rand() % G->handCount[player];
	}
	G->hand[player][G->handCount[player]] = G->hand[player][ctp_pos];
	G->handCount[player] = G->handCount[player] + 1;
	G->hand[player][ctp_pos] = cutpurse;
	
	// setup for coin test
	G->coins = coins_in_hand(player, G);
	int num_coins_before = G->coins;
	int num_coins_after = num_coins_before + 2;

	// get all the other player's coppers
	int* num_all_coppers_hand = (int*)malloc(sizeof(int) * G->numPlayers);
	int* num_all_coppers_discard = (int*)malloc(sizeof(int) * G->numPlayers);
	int* num_all_coppers_hand_after = (int*)malloc(sizeof(int) * G->numPlayers);
	int* num_all_coppers_discard_after = (int*)malloc(sizeof(int) * G->numPlayers);
	for(i = 0; i< G->numPlayers; i++)
	{
		num_all_coppers_hand[i] = num_copper_hand(i, G);
		num_all_coppers_discard[i] = num_copper_discard(i, G);
		if(i != player)
		{
			if(num_all_coppers_hand[i] > 0)
			{
				num_all_coppers_hand_after[i] = num_all_coppers_hand[i] - 1;
				num_all_coppers_discard_after[i] = num_all_coppers_discard[i] + 1;
			}
			else
			{
				num_all_coppers_discard_after[i] = num_all_coppers_discard[i];
			}
		}
		else
		{
			num_all_coppers_hand_after[i] = num_all_coppers_hand[i];
			num_all_coppers_discard_after[i] = num_all_coppers_discard[i];
		}
	}

	if(playCard(ctp_pos, 0, 0, 0, G) == -1)
	{
		printf("Error: playCard did not execute properly");
		return 1;
	}

	if(G->coins != num_coins_after)
	{
		printf("Error: Expected %d coins, got %d. played by%d\n",
				num_coins_after,
				G->coins,
				player);
		return 1;
	}

	for(i = 0; i < G->numPlayers; i++)
	{
		if(num_all_coppers_hand_after[i] != num_copper_hand(i, G))
		{
			printf("Error: Expected %d copper in player %d's hand, got %d\n",
					num_all_coppers_hand_after[i],
					i,
					num_copper_hand(i, G));
			return 1;
		}
		if(num_all_coppers_discard_after[i] != num_copper_discard(i, G))
		{
			printf("Error: Expected %d copper in player %d's discard, got %d playedby %d\n",
					num_all_coppers_discard_after[i],
					i,
					num_copper_discard(i, G),
					player);
			return 1;
		}
	}
	return 0;
}

int main()
{
	srand(time(NULL));
	int test_value = 0;
	int i;
	for(i = 0; i < 15000; i++)
	{
		if(testCutpurse() == 1)
		{
			test_value = 1;
		}
	}
	
	if(test_value == 1)
	{
		printf("Test Summary: FAIL\n");
	}
	else
	{
		printf("Test Summary: PASS\n");
	}
	return 0;
}
